//
//  HMOrdinaryCourseVC.m
//  优学荟管理系统
//
//  Created by 陈辉明 on 2017/12/25.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HMOrdinaryCourseVC.h"
#import "HMOrdinaryCourseCell.h"
#import "HMOrdinaryCourseModel.h"

@interface HMOrdinaryCourseVC () <UITableViewDataSource>


@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;



@end

@implementation HMOrdinaryCourseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTableView];
    [self setUpRefresh];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}

- (void)setUpTableView {
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HMOrdinaryCourseCell" bundle:nil] forCellReuseIdentifier:@"HMOrdinaryCourseCellId"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 120;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


#pragma mark - UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HMOrdinaryCourseModel *model = self.dataSource[indexPath.row];
    HMOrdinaryCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMOrdinaryCourseCellId" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    [cell.upButton addTarget:self action:@selector(upDownButtonActionID:) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;
}

#pragma mark ---- 上架方法的实现
-(void)upDownButtonActionID: (UIButton *)sender {
    
    HMOrdinaryCourseCell * cell = (HMOrdinaryCourseCell *)sender.superview.superview;
    HMOrdinaryCourseModel *model = cell.model;
    NSString *url = [NSString stringWithFormat:@"lesson.do?action=changeSellStatus&id=%@&sellStatus=1",model.Id];
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [HUD showSuccess:Obj[@"msg"]];
        if ([Obj[@"code"] isEqualToNumber:@1000]) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.tableView.mj_header beginRefreshing];
            });
        }
    } Fail:^(NSError *Error) {
        [HUD showError:NetErr];
    }];
    
    
    
    
    
}

#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}

#pragma mark - 网络请求
- (void)setNetWork{
    
    //http://192.168.0.226:8080/StarShine/ys/yxhUser.do?action=getLessonListStatusByParams&pageNumber=1&type=12
    
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=getLessonListStatusByParams&pageNumber=%ld&type=%@",(long)self.page,self.type];

    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
   
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [SVProgressHUD dismiss];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            NSDictionary *t = [Obj valueForKey:@"t"];
            NSArray *arr = [t valueForKey:@"dataList"];
            for (NSDictionary *dict in arr) {
                HMOrdinaryCourseModel *model = [HMOrdinaryCourseModel HMOrdinaryCourseModelWithDict:dict];
            [self.dataSource addObject:model];
                
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
        }
    
    } Fail:^(NSError *Error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
    
}

- (NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
    
    
}



@end
